<link type="text/css" rel="stylesheet" href="__PUBLIC__/css/validator.css"/>
<script type="text/javascript" src="__PUBLIC__/js/checkuser.js"></script>
<style>
    .close{font-size:34px;font-weight:400;color:#fff;}
    .close:hover{color:#fff;}
    input::-webkit-outer-spin-button,input::-webkit-inner-spin-button{
        -webkit-appearance:textfield;
    }
    input[type="number"]{
        -moz-appearance:textfield;
    }
</style>
<div class="modal-header" style="padding:3px 10px;">
    <b style="font-size:16px;">新增回款计划</b>
    <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
</div>
<div class="modal-body clearfix">
    <form id="ad_Form" class="form-horizontal m-t" method="post">
        <input type="hidden" name="num" id="nums" value="">
        <input type="hidden" name="business" id="businessname" value="">
        <input type="hidden" name="customer_id" id="customer_id" value="">
        <div class="col-sm-12">
                <div class="form-group">
                    <div class="col-sm-1">
                    </div>
                    <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>回款对应项目:</label>
                    <div class="col-sm-6">
                        <!--<input type="text"  readonly="readonly" value="{$de_po}" class="form-control"/>-->
                        <select name="business_id" id="business" class="form-control checkit" rell="对应项目" onchange="contractChanged();">
                            <option value="">--回款对应项目--</option>
                            <volist name="business" id="vo">
                                <option value="{$vo['business_id']}">{$vo['name']}</option>
                            </volist>
                        </select>
                    </div><span class="col-sm-1"></span>
                    <div class="col-sm-2 error_msg" id="businessTip"></div>
                </div>
            <div class="form-group">
                <div class="col-sm-1"></div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color: #FF0011;">*</span>回款对应客户:</label>
                <div class="col-sm-6">
                    <input type="text" required="required" name="customer" id="customer" class="form-control" value="" readonly="readonly" placeholder="--回款对应客户--">
                </div>
            </div>
            <!--<div class="form-group ">-->
                <!--<div class="col-sm-1">-->
                <!--</div>-->
                <!--<label class="control-label col-sm-3" for="name" style="width:110px;"><span style="color:#FF0011;">*</span>：</label>-->
                <!--<div class="col-sm-6">-->
                    <!--<div class="input-daterange input-group">-->
                        <!--<span class="input-group-addon" style="background-color:#ccc;">{$prefixion}</span>-->
                        <!--<input type="text" id="number" name="number" rel="require" class="form-control checkit" value="{$number}"/>-->
                        <!--<input type="hidden" id="prefixion" name="prefixion" value="{$prefixion}"/>-->
                    <!--</div>-->
                <!--</div>-->
                <!--&lt;!&ndash; <div class="col-sm-3 error_msg" id="full_nameTip"></div> &ndash;&gt;-->
            <!--</div>-->
            <div class="form-group ">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>计划回款金额：</label>
                <div class="col-sm-6">
                    <input type="number" id="planed_money" onblur="clean(this);" name="planed_money" rell="回款金额" class="form-control checkit" />
                </div><span class="col-sm-1">元</span>
                <div class="col-sm-2 error_msg" id="planed_moneyTip"></div>
            </div>

            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <div class="col-sm-5">
                    计划回款期次：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<button class="btn" type="button" onclick="minus();" style="border: solid gray 0.1px;">-</button>&nbsp;&nbsp;<span id="num">2</span>&nbsp;&nbsp;<button class="btn" type="button" onclick="add();" style="border: solid gray 0.1px;">+</button>
                </div>
            </div>
            <div class="form-group ">
                <div class="col-sm-1">
                </div>
                <div class="col-sm-6">
                    <div>第1次回款</div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>期次金额：</label>
                <div class="col-sm-6" >
                    <input id="money1" rell="期次金额" onblur="propertyCalculate(this);" name="money1" type="number" class="form-control checkit money">
                </div><span class="col-sm-1">元</span>
                <div class="col-sm-2 error_msg" id="money1Tip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>期次占比：</label>
                <div class="col-sm-6">
                    <input type="text" id="property1" readonly="readonly" name="property1" class="form-control checkit">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>回款时间：</label>
                <div class="col-sm-6">
                    <input type="date" id="time1" rell="回款时间" name="time1" class="form-control checkit">
                </div><span class="col-sm-1"></span>
                <div class="col-sm-2 error_msg" id="time1Tip"></div>
            </div>
            <div class="form-group ">
                <div class="col-sm-1">
                </div>
                <div class="col-sm-6">
                    <div>第2次回款</div>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>期次金额：</label>
                <div class="col-sm-6">
                    <input type="number" id="money2" name="money2" rell="期次金额" onblur="propertyCalculate(this);" class="form-control checkit  money">
                </div><span class="col-sm-1">元</span>
                <div class="col-sm-2 error_msg" id="money2Tip"></div>
            </div>
            <div class="form-group">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>期次占比：</label>
                <div class="col-sm-6">
                    <input type="text" id="property2" readonly="readonly" onblur="" name="property2" class="form-control checkit">
                </div>
            </div>
            <div class="form-group remark">
                <div class="col-sm-1">
                </div>
                <label class="control-label col-sm-3" style="width:110px;"><span style="color:#FF0011;">*</span>回款时间：</label>
                <div class="col-sm-6">
                    <input type="date" id="time2" rell="回款时间" name="time2" class="form-control checkit">
                </div><span class="col-sm-1"></span>
                <div class="col-sm-2 error_msg" id="time2Tip"></div>
            </div>
        </div>
        <div class="col-sm-offset-3 col-sm-10" id="msg" style="color: red;display: none;" > ！期次总金额不应超过计划总金额</div>
    </form>
</div>
<script>
    function clean(obj) {
        if(obj.value != '' && obj.value != 0){
            $("#planed_moneyTip").html("");
            $(".money").each(function () {
                var i = parseFloat($(this).val());
                if(isNaN(i)){
                    $(this).parent().parent().next().find("input").val("0.00%");
                }else {
                    var total = parseFloat(obj.value);
                    var property = (i/total*100).toFixed(2); 
                    $(this).parent().parent().next().find("input").val(parseFloat(property)+"%");
                }
            });
            var etotal = 0;
            $(".money").each(function(){
                var i = parseFloat($(this).val());
                if(! isNaN(i)){
                    etotal += i ;
                }
            });
            if(etotal > parseFloat(obj.value)){
                swal("期次总金额不应超过计划总金额","","warning");
                $("#msg").css("display","none");
            }
        }
        if(obj.value == 0) swal("计划总金额不为零","","warning");
    }
    //
    function propertyCalculate(obj) {
        var planed_money = $("#planed_money").val();
        if(planed_money == '' || planed_money == 0){
            $("#planed_moneyTip").html("<span style='color:#FF0011;'>请先填写计划金额(非零)！</span>")
        }else {
            $("#planed_moneyTip").html("");
            if(obj.value != ''){
                var  property = parseFloat(((obj.value / planed_money )*100)).toFixed(2);
                var attr = obj.getAttribute('id');
                var num1 = parseInt(attr.substring(5));
                var str = "#property"+num1;
                $(str).val(property+"%");
            }
            var ta  = 0;
            $(".money").each(function () {
                if(!Number($(this).val()))
                    ta += 0;
                else
                    ta += parseFloat($(this).val());
            });
            if(ta>planed_money)
                // $("#msg").css('display',"block");else $("#msg").css('display',"none");
                swal("期次总金额不应超过计划总金额","","warning");
        }
    }

    function contractChanged() {
        var bvalue = $("#business").val();
        $.ajax({
            type: "post",
            async:false,
            url: "{:U('return/contractChanged')}",
            data: {business_id:bvalue},
            dataType: "json",
            success : function(result){
                $("#customer").val(result.customer_name);
                $("#business_id").val(result.business_id);
                $("#customer_id").val(result.customer_id);
            }
        });
    }
    var num = 2;
    function minus() {
        if(num>2){
            $(".remark").last().prev().remove();
            $(".remark").last().prev().remove();
            $(".remark").last().prev().remove();
            $(".remark").last().remove();
            num--;
        }else {
            swal("至少有两条回款期次！","","warning");
        };
        $("#num").html(num);
    }
    function add() {
        num ++;
        $(".remark").last().after("<div class='form-group'><div class='col-sm-1'></div><div class='col-sm-6'><div>第"+num+"次回款</div></div></div>" +
            "<div class='form-group'><div class='col-sm-1'></div><label class='control-label col-sm-3' style='width: 110px;'><span style='color: #FF0011;'>*</span>期次金额:</label>" +
            "<div class='col-sm-6'><input id='money"+num+"' name='money"+num+"' rell='期次金额' onblur='propertyCalculate(this);' type='number' class='form-control checkit money'></div><span class='col-sm-1'>元</span><div class='col-sm-2 error_msg' id='money"+num+"Tip'></div></div>" +
            "<div class='form-group'><div class='col-sm-1'></div><label class='control-label col-sm-3' style='width: 110px;'><span style='color: #FF0011;'>*</span>期次占比:</label>" +
            "<div class='col-sm-6'><input type='text' readonly='readonly' id='property"+num+"' name='property"+num+"' class='form-control checkit'></div></div>" +
            "<div class='form-group remark'><div class='col-sm-1'></div><label class='control-label col-sm-3' style='width: 110px;'><span style='color: #FF0011;'>*</span>回款时间:</label>" +
            "<div class='col-sm-6'><input type='date' id='time"+num+"' name='time"+num+"' rell='回款时间' class='form-control checkit'></div><span class='col-sm-1'></span><div class='col-sm-2  error_msg' id='time"+num+"Tip'></div></div>");
        $("#num").html(num);
    }
    $('#number').blur(function(){
        var number = $(this).val();
        var prefixion = $('#prefixion').val();
        var user_id = "";
        $.ajax({
            type: "post",
            async:false,
            url: "{:U('user/yanchong')}",
            data: {number:number,user_id:user_id,prefixion:prefixion},
            dataType: "json",
            success : function(result){
                if(result.status != 1){
                    swal('员工编号不能重复！','',"error");
                    return false;
                }else{
                    return true;
                }
            }
        });
    });
    $(document).ready(function(){
        $('.date').datepicker({
            format: "yyyy-mm-dd",
            todayBtn: "linked",
            keyboardNavigation: false,
            forceParse: false,
            calendarWeeks: true,
            autoclose: true
        });
        $(document).ready(function(){
            $("#department").change(function(){
                checkform('#role');
            });
            $("#role").change(function(){
                checkform('#role');
            });
        });

        $("#adbtn").click(function(){
            $("#nums").val(num);
            var bname = $("#business option:selected").html();
            $("#businessname").val(bname);
            var tp = parseFloat($("#planed_money").val());
            var ta  = 0.00;
            $(".money").each(function () {
                ta += parseFloat($(this).val());
            });
            var flag = true;
            if(before_submit() && ta>tp) {
                flag = false;
                swal("期次总金额不应超过计划总金额","","warning");
            }
            $(".checkit").each(function () {
                var id = "#"+$(this).attr('id')+"Tip";
               if($(this).val()=='' || $(this).val()==0){
                   input_msg = false;
                   var mes = $(this).attr('rell');
                   $(id).html("<span style='color: red;'>*请填写"+mes+"</span>");
               }else {
                   input_msg = true ;
                   $(id).html("");
               }
            });
            if(ta>tp) $("#planed_moneyTip").html("<span style='color: red;'>*!期次总金额不应超过计划总金额</span>");else $("#planed_moneyTip").html("");
            if(before_submit() && flag){
                if(ta<tp){
                    swal({
                            title: "您确定要保存当前计划吗？",
                            text: "期次总金额 未达到 计划回款总金额",
                            type: "warning",
                            showCancelButton: true,
                            confirmButtonColor: "#DD6B55",
                            confirmButtonText: "是的，我要保存！",
                            cancelButtonText:'让我再考虑一下…',
                            closeOnConfirm:false,
                            closeOnCancel:false
                        },
                        function(isConfirm){
                            if (isConfirm) {
                                $.ajax({
                                    type: "POST",
                                    url: "{:U('plan_add')}",
                                    data:$("#ad_Form").serialize(),
                                    async: true,
                                    success: function(data) {
                                        if(data.status == 1){
                                            swal({
                                                title: "添加成功！",
                                                text: "跳转中!",
                                                type: "success"
                                            });
                                            $('#Modal').modal('hide');
                                            top.location.reload();
                                        }else{
                                            swal({
                                                title: "添加失败!",
                                                text: data.info,
                                                type: "error"
                                            });
                                        }
                                    }
                                });
                            } else {
                                swal("已取消","您取消了添加操作！","error");
                            }
                        })
                }else
                $.ajax({
                    type: "POST",
                    url: "{:U('plan_add')}",
                    data:$("#ad_Form").serialize(),
                    async: true,
                    success: function(data) {
                        if(data.status == 1){
                            swal({
                                title: "添加成功！",
                                text: "跳转中!",
                                type: "success"
                            });
                            $('#Modal').modal('hide');
                            top.location.reload();
                        }else{
                            swal({
                                title: "添加失败!",
                                text: data.info,
                                type: "error"
                            });
                        }
                    }
                });

            }else{
                var item_id = [];
                $('.checkit').each(function(k,v){
                    checkform(v);
                    item_id.push($(v).attr('id'));
                });
                $.each(item_id,function(k,v){
                    if($('#'+v+'Tip').html() == ''){
                        item_id.splice(k,1);
                    }
                });
                $('#'+item_id[0]).focus();
                return false;
            }
        });
    });
    $('#role').click(
        function(){
            department_id = $('#department').val();
            if(department_id == ''){
                swal({
                    title: "温馨提示",
                    text: "{:L('SELECT_DEPARTMENT_FIRST')}"
                })
                return false;
            }
        }
    );
</script>
<div class="modal-footer" style="padding:8px 22px;">
    <button type="button" id="adbtn" class="btn btn-primary">保存</button>
    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
</div>